#include <stdio.h>
#include <malloc.h>

typedef int ElemType;
typedef struct LNode  
{
	ElemType data;
	struct LNode *next;		//指向后继结点
} LinkNode;	


void CreateListR(LinkNode *&L,ElemType a[],int n)
//尾插法建立单链表
{
	LinkNode*r = NULL;
	
	for(int i = 0;i<n;i++)
	{
		LinkNode*s = (LinkNode*)malloc(sizeof(LinkNode));
		s->data = a[i];
		s->next = NULL;
		if(r==NULL)
		{
			L = s;
			r = L;
		}
		else{
			r->next = s;
			r = s;
		}
	}
}

void DispList(LinkNode *L)
{
	if(L==NULL) return;
	if(L!=NULL)
	{
		printf("%d ",L->data);
		DispList(L->next);
	}
}

void DestroyList(LinkNode *&L)
{
	if(L==NULL) return;
	DestroyList(L->next);
	free(L);
}

void Delete(LinkNode*& L,ElemType x)
{
	if(L==NULL) return ;
	LinkNode*s;
	if(L->data==x)
	{
		s = L;
		L = L->next;
		free(s);
		Delete(L,x);
	}
	else Delete(L->next,x);
}



int main()
{
	ElemType a[8] = {5,6,2,2,2,3,8,9};
	LinkNode* L;
	CreateListR(L,a,8);
	printf("初始化后链表中元素为：\n");
	DispList(L);
	printf("\n");
	ElemType x = 0;
	printf("要删除哪个元素：");
	scanf("%d",&x);
	Delete(L,x);
	DispList(L);
	
	DestroyList(L);
	
	return 0;
}
